﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchoolApplication.Models;
using GameSchoolApplication.Repository;
using GameSchoolApplication.ViewModels;

namespace GameSchoolApplication.Controllers
{
    public class LevelController : Controller
    {
        private GameSchoolWebContext _db = new GameSchoolWebContext();
        private GameRepository _game = new GameRepository();
       
		
        /// <summary>
        /// Returns the game, all of its levels that are not completed and the current 
        /// level that the student is on.
        /// </summary>
        /// <param name="gameId"></param>
        /// <returns></returns>
        public ActionResult Index(int gameId)
        {
			LevelViewModel model = new LevelViewModel();
			model.Game = _game.GetGameById(gameId);
			model.GameLevels = _game.GetGameLevels(gameId);
            model.CurrentLevel = model.Game.Levels.OrderBy(l => l.LevelId).Where(l => !l.Complete).FirstOrDefault();
            return View(model);
        }

		/// <summary>
		/// Gets a level by id
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
        public ActionResult Details(int id)
        {
            Level level = _game.GetLevelById(id);
            return View(level);
        }

		/// <summary>
		/// Edits a specific level
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
        [HttpGet]
        public ActionResult Edit(int id)
        {
            Level level = _game.GetLevelById(id);
            return View(level);
        }

		/// <summary>
		/// Posts back the edits we have made to the level
		/// </summary>
		/// <param name="level"></param>
		/// <returns></returns>
        [HttpPost]
        public ActionResult Edit(Level level)
        {
            if (ModelState.IsValid)
            {
                _game.UpdateLevel(level);
                return RedirectToAction("Index", new { gameId = level.GameId });
            }
			return View(level);
        }

		/// <summary>
		/// Create a new level
		/// </summary>
		/// <param name="gameId"></param>
		/// <returns></returns>
        [HttpGet]
        public ActionResult Create(int gameId)
        {
            return View(new Level {GameId = gameId});
        }

		/// <summary>
		/// Adds a level to the repository
		/// </summary>
		/// <param name="level"></param>
		/// <returns></returns>
        [HttpPost]
        public ActionResult Create(Level level)
        {
            if (ModelState.IsValid)
            {
                _game.AddLevel(level);
                return RedirectToAction("Index", new { gameId = level.GameId });
            }
            else
            {
                return View(level);
            }
        }


		/// <summary>
		/// Gets the current level for the student
		/// </summary>
		/// <param name="gameId"></param>
		/// <returns></returns>
        [HttpGet]
        public ActionResult Active(int gameId)
        {
            Person student = _db.Persons.Where(p => p.UserName == User.Identity.Name).SingleOrDefault();
            StudentLevelPoints current = student.CurrentLevels.Where(cl => cl.Level.GameId == gameId).SingleOrDefault();
            return View(current);
        }

		/// <summary>
		/// Lets the teacher close a level
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
        [HttpPost]
        public ActionResult CloseLevel(int id)
        {
            Level level = _game.GetLevelById(id);
            _game.CloseLevel(level);
            return RedirectToAction("Index", new { gameId = level.GameId });
        }

		protected override void Dispose(bool disposing)
		{
			_db.Dispose();
			_game.Dispose();
			base.Dispose(disposing);
		} 
    }
}
